package com.example.internetplus.controller.globalException.advcie;

import com.example.internetplus.controller.globalException.exception.BusinessException;
import com.example.internetplus.controller.globalException.exception.LoginException;
import com.example.internetplus.controller.globalException.exception.SystemException;
import com.example.internetplus.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import static com.example.internetplus.constrants.ExceptionConstants.SYSTEM_UNKOWN_ERROR;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionAdvice {
    @ExceptionHandler(BusinessException.class)
    public Result doBusinessException(BusinessException e){
        return Result.defeat(e.getMessage(),e.getCode());
    }

    @ExceptionHandler(SystemException.class)
    public Result doSystemException(SystemException e){
        log.error(e.getMessage(),e.getCode());
        return Result.defeat(e.getMessage(),e.getCode());
    }

    @ExceptionHandler(LoginException.class)
    public Result doLoginExceptioin(LoginException e){
        log.error(e.getMessage(),e.getCode());
        return Result.defeat(e.getMessage(),e.getCode());
    }
    @ExceptionHandler(Exception.class)
    public Result doException(Exception e){
        log.error("发现未知错误" + e.getMessage());
        return Result.defeat("系统繁忙请等待",SYSTEM_UNKOWN_ERROR);
    }

}
